package com.wnxy.admin.controller;

import cn.hutool.core.util.RandomUtil;
import com.wnxy.admin.common.ResponseResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/email")
public class EmailController {

    private final JavaMailSender javaMailSender;
    private final RedisTemplate redisTemplate;
    private final RabbitTemplate rabbitTemplate;
    @Value("${username}")
    private String userName;

    public EmailController(JavaMailSender javaMailSender, RedisTemplate redisTemplate, RabbitTemplate rabbitTemplate) {
        this.javaMailSender = javaMailSender;
        this.redisTemplate = redisTemplate;
        this.rabbitTemplate = rabbitTemplate;
    }

    @GetMapping("/send")
    public ResponseResult send(String targetUrl) {
        String code = RandomUtil.randomString(4);
        Map<String, String> msg = new HashMap<>();
        msg.put("from", userName);
        msg.put("to", targetUrl);
        msg.put("subject", "验证码");
        msg.put("code", code);
        rabbitTemplate.convertAndSend("email_queue", msg);
        return ResponseResult.ok();
    }
}


